import path from "path";
import os from "os";
import { app } from "electron";
import { getLogger } from "@/utils/index";
import { getEnvPorts } from "@/jc-electron-tools/env";
import type { Logger } from "@/jc-electron-tools/types";

// Application Name
const APP_NAME = "jc-admin-desktop";

const BASE_DIR = path.resolve(__dirname, "..");
const SOURCES_DIR = path.join(BASE_DIR, "sources");

const APP_BASE =
    process.env.NODE_ENV === "development"
        ? path.resolve(path.resolve(__dirname, "../"), "../")
        : app.getAppPath();

// APP Icon Path
const APP_ICON_PATH =
    process.env.NODE_ENV === "development"
        ? path.join(path.join(BASE_DIR, "assets"), "jcstudio_256x256.ico")
        : path.join(path.join(APP_BASE, "assets"), "jcstudio_256x256.ico");

// VITE ENV 将用于获取开发模式下调试时的端口
const VITE_ENVS_DIR = path.resolve(APP_BASE, "VITE_ENVS");
const VITE_ENVS_FILE = path.join(VITE_ENVS_DIR, ".env");

// LOGS
const LOG_DIR = app.getPath("logs");
const APP_LOG_PATH = path.join(LOG_DIR, "app.log");
const APP_DATAFILE_LOG_PATH = path.join(LOG_DIR, "datefile.log");
const NET_LOG_PATH = path.join(LOG_DIR, "net.log");

// System user AppData
const APP_DATAS_BASE_DIR = path.join(
    path.join(path.join(os.homedir(), "AppData"), "Roaming"),
    APP_NAME
);
// Log File Save Directory
app.setAppLogsPath(path.join(APP_DATAS_BASE_DIR, "logs"));
const logger: Logger = getLogger();

// Database - SQLite File
const DB_ABSOLUTE_PATH = path.join(APP_DATAS_BASE_DIR, "rundata.sqlite3");

const DEFAULT_DATA_HOME = path.join(APP_DATAS_BASE_DIR, "notes");

const WINDOW_PORT = getEnvPorts(VITE_ENVS_FILE);

const RENDER_BUILDS_DIR = path.join(APP_BASE, "renders");

const RENDER_APP_PATH = {
    user: path.join(RENDER_BUILDS_DIR, "user"),
    desktop: path.join(RENDER_BUILDS_DIR, "desktop"),
    admin: path.join(RENDER_BUILDS_DIR, "admin"),
    music: path.join(RENDER_BUILDS_DIR, "music"),
};

const RENDER_HTML_PATH = {
    user: path.join(RENDER_APP_PATH.user, "index.html"),
    desktop: path.join(RENDER_APP_PATH.desktop, "index.html"),
    admin: path.join(RENDER_APP_PATH.admin, "index.html"),
    music: path.join(RENDER_APP_PATH.music, "index.html"),
};

export {
    APP_NAME,
    BASE_DIR,
    SOURCES_DIR,
    VITE_ENVS_DIR,
    LOG_DIR,
    APP_DATAFILE_LOG_PATH,
    NET_LOG_PATH,
    APP_DATAS_BASE_DIR,
    APP_LOG_PATH,
    logger,
    DB_ABSOLUTE_PATH,
    APP_ICON_PATH,
    DEFAULT_DATA_HOME,
    WINDOW_PORT,
    RENDER_HTML_PATH,
};
